//index.js
//获取应用实例
const app = getApp()

var fileData = require('../../utils/data.js');
let servicePath = require("../../utils/servicePath.js");
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
   grids: [],
   doctors:[],
   appDomain : app.globalData.appDomain
  },
 
  changeAutoplay: function (e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },

  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    this.setData({
      grids: fileData.kslist()
     })
     var _this = this;
     wx.request({
       url: servicePath.HotDoctorService(),
       method:"GET",
       success:function(res){
        if(res.data.code == 200){
          console.log(res.data);
          _this.setData({
            doctors:res.data.data
          })
        }
       }
     })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  tapyuyue:function(){
    // let backUser = app.globalData.backUserInfo;
    // if(backUser == null) {
    //   wx.navigateTo({
    //     url: '../register/register',
    //   })
    // }else {
    //   wx.navigateTo({
    //     url: '../../indexviews/preview/preview',
    //   })
    // }
       wx.navigateTo({
        url: '../../indexviews/preview/preview',
      })
  },
  //分享
  onShareAppMessage:function(){
    return {
      title: '南极星养老服务',
      desc: '最专业的养老服务在线机构!',
      path: '/index/index'
    }
  },
  //给医生打电话
  callDoctor: function () {
    wx.showToast({
      title: '功能暂未开通',
      icon: 'none',
      duration: 2000
    })
  },
  clickDetail:function(e){
    wx.navigateTo({
      url: '../../indexviews/doctorView/doctorView?id=' + e.currentTarget.dataset.id
    })
  },
  //跟医生发起聊天
  chatDoctor: function (e) {
    console.log(e.currentTarget);
    var toName = e.currentTarget.dataset.toname;
    var toId = e.currentTarget.dataset.toid;
    wx.navigateTo({
      url: '../chat/chat?toName='+toName+"&toId="+toId,
    })
  },
  //代办住院
  taphospital:function(){
    wx.navigateTo({
      url: '../../indexviews/hospital/hospital',
    })
  },
  tapcheck:function(){
    wx.navigateTo({
      url: '../../indexviews/check/check',
    })
  }
})
